<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="merchantId">
        <el-select v-model="queryParams.merchantId" style="width: 220px" placeholder="请选择商户"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="mer in merchantList" :key="mer.id"
                     :label="mer.defindName ? mer.defindName : mer.merchantName"
                     :value="mer.merchantId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="region">
        <el-input v-model="queryParams.region" placeholder="请输入站点" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item prop="shopId">
        <el-input v-model="queryParams.shopId" placeholder="请输入店铺Id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item prop="discountName">
        <el-input v-model="queryParams.discountName" placeholder="请输入名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleSync">同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="discountList">
      <el-table-column label="ID" align="center" prop="discountId" v-if="false"/>
      <el-table-column label="全球店铺Id" align="center" prop="merchantId" v-if="false"/>
      <el-table-column label="站点" align="center" prop="region"/>
      <el-table-column label="名称" align="center" prop="discountName"/>
      <el-table-column prop="isPublish" align="center" label="用于发布">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isPublish" active-color="#13ce66" inactive-color="#ff4949" :active-value="true"
                     :inactive-value="false" @change="updateIsPublish(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="店铺Id" align="center" prop="shopId"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope"><span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span></template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope"><span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span></template>
      </el-table-column>
      <el-table-column label="展示于" align="center" prop="source">
        <template slot-scope="scope"> {{ scope.row.source == 0 ? "全渠道" : "非全渠道" }}</template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>
<script>
import {
  listDiscount,
  syncDiscount,
  updateDiscount,
} from "@/api/store/discount";
import {listAllMerchant} from "@/api/shop/merchant";
import Cookies from "js-cookie";

export default {
  name: "Discount",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      //全球商户列表
      merchantList: [],
      // 总条数
      total: 0,
      // 折扣活动表格数据
      discountList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        discountId: null,
        merchantId: null,
        region: null,
        shopId: null,
        discountName: null,
        startTime: null,
        endTime: null,
        source: null,
        isPublish: null,
      },
    };
  },
  created() {

    this.getMerchantList();
  },
  methods: {
    /** 查询折扣活动列表 */
    getList() {
      this.loading = true;
      listDiscount(this.queryParams).then((response) => {
        this.discountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMerchantList() {
      listAllMerchant().then((response) => {
        this.merchantList = response.data;
        //默认选中
        if (Cookies.get('merchantId')) {
          this.queryParams.merchantId = +Cookies.get('merchantId');
        }
        this.getList();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.merchantId) {
        Cookies.set('merchantId', this.queryParams.merchantId);
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 同步按钮操作 */
    handleSync() {
      this.loading = true;
      syncDiscount(this.queryParams).then((response) => {
        this.getList();
        this.loading = false;
      });
    },
    /** 修改操作 */
    updateIsPublish(data) {
      //console.log("最新信息", data);
      let {discountId, isPublish} = data;
      updateDiscount({discountId, isPublish}).then((response) => {
        this.$message({
          type: "success",
          message: "状态更新成功",
        });
      });
    },
  },
};
</script>
